package edu.ahpu.propertymanage.controller;

import edu.ahpu.propertymanage.dataclass.PageSet;
import edu.ahpu.propertymanage.dataclass.PropCostData;
import edu.ahpu.propertymanage.pojo.PropertyTage;
import edu.ahpu.propertymanage.service.PropCostService;
import edu.ahpu.propertymanage.service.PropertyTageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/prop")
public class PropCostController {

    @Autowired
    PropCostService propCostService;

    @Autowired
    PropertyTageService propertyTageService;

    @RequestMapping("showpropcost")
    public List showPropCost(@RequestBody PageSet pageSet){
        pageSet.setTotal(propCostService.countPropCost());
        List<PropCostData> list = propCostService.showPropCost(pageSet);
        int tage = list.get(0).getPropPrice();
        PropertyTage propertyTage = propertyTageService.getById(tage);
        Float price = propertyTage.getPrice();
        for (PropCostData cost:list) {
            cost.setPrice(price);
            cost.setTotal(pageSet.getTotal());
            cost.setSize(pageSet.getSize());
            cost.setCurrent(pageSet.getCurrent());
            cost.setPropCost(price*cost.getOwnerHome());
            if (new Date().getTime()>cost.getPropEndDate().getTime()){
                cost.setIsSubmit(false);
            }else
                cost.setIsSubmit(true);
        }
        return list;
    }
}
